package com.ebpm.designer.line
{
	import com.ebpm.designer.pvm.EndEvent;
	import com.ebpm.designer.pvm.StartEvent;
	import com.ebpm.designer.WF;
	import com.ebpm.designer.item.DownItem;
	import com.ebpm.designer.item.Item;
	import com.ebpm.designer.item.LeftItem;
	import com.ebpm.designer.item.RightItem;
	import com.ebpm.designer.item.TopItem;
	
	import mx.controls.Alert;

	public class LineUtil
	{
		public function LineUtil()
		{
			
			
		}
		
		public static function newLine(fromItem:Item, toItem:Item):void {
			if(fromItem == toItem || fromItem.nodeContainer == toItem.nodeContainer) {
				return;
			}
			if(toItem.nodeContainer.mission is StartEvent) return ;
			if(fromItem.nodeContainer.mission is EndEvent) return ;
			if(fromItem.nodeContainer.mission is StartEvent) {
				for(var i:int=0;i<fromItem.nodeContainer.items.length; i++) {
					var item:Item = fromItem.nodeContainer.items.getItemAt(i) as Item;
					for(var k:int=0;k<item.sourceLines.length;k++) {
						return ;
					}
				}
			}
			
			for(var i:int=0; i<toItem.nodeContainer.items.length; i++) {
				var item:Item = toItem.nodeContainer.items.getItemAt(i) as Item;
				for(var k:int=0; k<item.targetLines.length; k++) {
					var l:Line = (item.targetLines.getItemAt(k) as Line);
					if(l.sourceItem.nodeContainer._id == fromItem.nodeContainer._id) {
//						Alert.show("节点之间只能存在一个链接");
						return ;
					}					
				}
			}
			
			var fromX:Number = fromItem.getPageX();
			var fromY:Number = fromItem.getPageY();
			var toX:Number = toItem.getPageX();
			var toY:Number = toItem.getPageY();
			var line:Line;
			//如果是 从底部拉线
			if(fromItem is DownItem) {
				if(toItem is RightItem) {
					if(toX > fromX) {
						Alert.show("不允许操作");
						return ;
					}
					line = new DRLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is LeftItem) {
					if(toX < fromX) {
						Alert.show("不允许操作");
						return ;
					}
					line = new DLLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is TopItem) {
					line = new DTLine().paint(fromX, fromY, toX, toY);
				}
				//连点为down的有两个方向
				if(toItem is DownItem) {
					//判断是否越界,跟在DDLine里面一样
					if(Math.abs((toX + (WF.ITEM_SIZE / 2) - fromX + (WF.ITEM_SIZE / 2))) <= (toItem.nodeContainer.width / 2)) {
						if(fromItem.nodeContainer.getDownLineY() < toItem.nodeContainer.getDownLineY()) {
							line = new LLLine().paint(fromItem.nodeContainer.getLeftLineX(), fromItem.nodeContainer.getLeftLineY(),
								toItem.nodeContainer.getLeftLineX(), toItem.nodeContainer.getLeftLineY());
							line.sourceItem = fromItem.nodeContainer.leftItem;
							fromItem.nodeContainer.leftItem.sourceLines.addItem(line);
							toItem.nodeContainer.leftItem.targetLines.addItem(line);
							line.targetItem = toItem.nodeContainer.leftItem;
							return ;
						} else {
							line = new RRLine().paint(fromItem.nodeContainer.getRightLineX(), fromItem.nodeContainer.getRightLineY(),
								toItem.nodeContainer.getRightLineX(), toItem.nodeContainer.getRightLineY());
							line.sourceItem = fromItem.nodeContainer.rightItem;
							fromItem.nodeContainer.rightItem.sourceLines.addItem(line);
							toItem.nodeContainer.rightItem.targetLines.addItem(line);
							line.targetItem = toItem.nodeContainer.rightItem;
							return ;
						}
					} else {
						if(fromY > toY) {
							line = new DDLine();
							line.paint(fromX, fromY, toX, toY);
						} else {
							line = new DDLine().paint(fromX, fromY, toX, toY);
						}
					}
				}
			}
			
			//从左部拉线
			if(fromItem is LeftItem) {
				if(toItem is TopItem) {
					if(toY < fromY) {
						Alert.show("l2不允许操作");
						return ;
					}
					line = new LTLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is DownItem) {
					if(fromY < toY) {
						Alert.show("l3不允许操作");
						return ;
					}
					line = new LDLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is RightItem) {
					line = new LRLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is LeftItem) {
					if(fromX < toX) {
						line = new LLLine();
						line.paint(fromX, fromY, toX, toY);
					} else {
						line = new LLLine().paint(fromX, fromY, toX, toY);
					}
				}
			}
			
			//从右部拉线
			if(fromItem is RightItem) {
				if(toItem is TopItem) {
					if(toY < fromY) {
						Alert.show("l2不允许操作");
						return ;
					}
					line = new RTLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is DownItem) {
					if(fromY < toY) {
						Alert.show("l3不允许操作");
						return ;
					}
					line = new RDLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is RightItem) {
					if(fromX > toX) {
						line = new RRLine();
						line.paint(fromX, fromY, toX, toY);
					} else {
						line = new RRLine().paint(fromX, fromY, toX, toY);
					}
				}
				if(toItem is LeftItem) {
					line = new RLLine().paint(fromX, fromY, toX, toY);
				}
			}
			
			//从上部拉线
			if(fromItem is TopItem) {
				if(toItem is RightItem) {
					if(toX > fromX) {
						Alert.show("不允许操作");
						return ;
					}
					line = new TRLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is LeftItem) {
					if(toX < fromX) {
						Alert.show("不允许操作");
						return ;
					}
					line = new TLLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is DownItem) {
					line = new TDLine().paint(fromX, fromY, toX, toY);
				}
				if(toItem is TopItem) {
					//查看是否越界
					if(Math.abs((toX - fromX)) <= (toItem.nodeContainer.width / 2)) {
						if(fromY < toY) {
							line = new RRLine().paint(fromItem.nodeContainer.getRightLineX(), fromItem.nodeContainer.getRightLineY(),
								toItem.nodeContainer.getRightLineX(), toItem.nodeContainer.getRightLineY());
							line.sourceItem = fromItem.nodeContainer.rightItem;
							fromItem.nodeContainer.rightItem.sourceLines.addItem(line);
							toItem.nodeContainer.rightItem.targetLines.addItem(line);
							line.targetItem = toItem.nodeContainer.rightItem;
							return ;
						} else {
							line = new LLLine().paint(fromItem.nodeContainer.getLeftLineX(), fromItem.nodeContainer.getLeftLineY(),
								toItem.nodeContainer.getLeftLineX(), toItem.nodeContainer.getLeftLineY());
							line.sourceItem = fromItem.nodeContainer.leftItem;
							fromItem.nodeContainer.leftItem.sourceLines.addItem(line);
							toItem.nodeContainer.leftItem.targetLines.addItem(line);
							line.targetItem = toItem.nodeContainer.leftItem;
							return ;
						}
					} 
					else {
						line = new TTLine().paint(fromX, fromY, toX, toY);
					}
				}
			}
			
			
			
			//往item 添加线
			line.sourceItem = fromItem;
			line.targetItem = toItem;
			fromItem.sourceLines.addItem(line);
			toItem.targetLines.addItem(line);
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}